package com.yy.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.yy.common.annotation.Log;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.enums.BusinessType;
import com.yy.common.utils.poi.ExcelUtil;
import com.yy.domain.AssetBuilding;
import com.yy.domain.AssetHouse;
import com.yy.domain.AssetHouseBlock;
import com.yy.service.IAssetBuildingService;
import com.yy.service.IAssetHouseBlockService;
import com.yy.service.IAssetHouseService;

import com.yy.vo.AssetHouseImpVo;
import com.yy.vo.AssetHouseVo;
import com.yy.vo.TreeOptions;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 商铺管理Controller
 * 
 * @author leige
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/asset/house")
public class AssetHouseController extends BaseController
{
    @Autowired
    private IAssetHouseService assetHouseService;

    @Autowired
    private IAssetBuildingService assetBuildingService;
    @Autowired
    private IAssetHouseBlockService assetHouseBlockService;



    /**
     * 查询商铺管理列表
     */
    @PreAuthorize("@ss.hasPermi('asset:house:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetHouse assetHouse)
    {
        startPage();
        List<AssetHouseVo> list = assetHouseService.selectAssetHouseList(assetHouse);
        return getDataTable(list);
    }

    /**
     * 导出商铺管理列表
     */
    @PreAuthorize("@ss.hasPermi('asset:house:export')")
    @Log(title = "商铺管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetHouse assetHouse)
    {
        List<AssetHouseVo> list = assetHouseService.selectAssetHouseList(assetHouse);
        ExcelUtil<AssetHouseVo> util = new ExcelUtil<AssetHouseVo>(AssetHouseVo.class);
        util.exportExcel(response, list, "商铺管理数据");
    }

    /**
     * 获取商铺管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:house:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(assetHouseService.selectAssetHouseById(id));
    }

    /**
     * 新增商铺管理
     */
    @PreAuthorize("@ss.hasPermi('asset:house:add')")
    @Log(title = "商铺管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetHouse assetHouse)
    {
        //处理name ==完整编号=block_code-building_code-floor-code
        AssetHouseBlock assetHouseBlock = assetHouseBlockService.selectAssetHouseBlockById(assetHouse.getBlockId());
        AssetBuilding assetBuilding = assetBuildingService.selectAssetBuildingById(assetHouse.getBuildingId());
        String name=assetHouseBlock.getCode()+"-"+assetBuilding.getNumber()+"-"+assetHouse.getFloor()+"-"+assetHouse.getCode();
        assetHouse.setName(name);
        //设置创建时间创建人
        assetHouse.setCreateBy(getUsername());
        return toAjax(assetHouseService.insertAssetHouse(assetHouse));
    }

    /**
     * 修改商铺管理
     */
    @PreAuthorize("@ss.hasPermi('asset:house:edit')")
    @Log(title = "商铺管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetHouse assetHouse)
    {
        //处理name ==完整编号=block_code-building_code-floor-code
        AssetHouseBlock assetHouseBlock = assetHouseBlockService.selectAssetHouseBlockById(assetHouse.getBlockId());
        AssetBuilding assetBuilding = assetBuildingService.selectAssetBuildingById(assetHouse.getBuildingId());
        String name=assetHouseBlock.getCode()+"-"+assetBuilding.getNumber()+"-"+assetHouse.getFloor()+"-"+assetHouse.getCode();
        assetHouse.setName(name);
        //设置修改人
        assetHouse.setUpdateBy(getUsername());
        return toAjax(assetHouseService.updateAssetHouse(assetHouse));
    }

    /**
     * 删除商铺管理
     */
    @PreAuthorize("@ss.hasPermi('asset:house:remove')")
    @Log(title = "商铺管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(assetHouseService.deleteAssetHouseByIds(ids));
    }

    /**
     * 获取所有的商铺信息不分页
     */
    @GetMapping("getAllHouses")
    public AjaxResult getAllHouses(){
        List<AssetHouseVo> assetHouseVos = this.assetHouseService.selectAssetHouseList(new AssetHouse());
        return AjaxResult.success(assetHouseVos);
    }
    /**
     * 根据商铺ID查询商铺信息
     {
     "msg": "操作成功",
     "code": 200,
     "data": {
     "houseName": "JRG-B26-1-102",
     "buildingName": "B27栋",
     "code": "102",
     "address": "光谷大道109号",
     "blockName": "光谷金融港",
     "floor": "1"
     }
     }
     */
    @GetMapping("getAllResourceByHouseId/{id}")
    public AjaxResult getAllResourceByHouseId(@PathVariable Long id){
        AssetHouseImpVo assetHouseImpVo = this.assetHouseService.queryAllResourceByHouseId(id);
        return AjaxResult.success(assetHouseImpVo);
    }

    /**
     * 查询商圈、楼栋、商铺的三级联数据
     */
    @GetMapping("getAllHouseCascade")
    public AjaxResult getAllHouseCascade(){
        //查询所有商圈
        List<AssetHouseBlock> assetHouseBlocks = assetHouseBlockService.selectAssetHouseBlockList(new AssetHouseBlock());
        //查询所有的楼栋
        List<AssetBuilding> assetBuildings=assetBuildingService.selectAssetBuildingList(new AssetBuilding());
        //查询所有的商铺
        List<AssetHouseVo> assetHouses=this.assetHouseService.selectAssetHouseList(new AssetHouse());
        List<TreeOptions> treeOptions=new ArrayList<>();
        //开始组装数据
        for (AssetHouseBlock houseBlock : assetHouseBlocks) {
            TreeOptions options=new TreeOptions(houseBlock.getId().toString(),houseBlock.getName());
            for (AssetBuilding building : assetBuildings) {
                if(building.getBlockId().equals(houseBlock.getId())){
                    TreeOptions buildingOptions=new TreeOptions(building.getId().toString(),building.getName());
                    for (AssetHouseVo house : assetHouses) {
                        if(house.getBuildingId().equals(building.getId())){
                            //把house组装成TreeOptions放到buildingOptions的children里面
                            TreeOptions houseOptions=new TreeOptions(house.getId().toString(),house.getName());
                            buildingOptions.getChildren().add(houseOptions);
                        }
                    }
                    //把buildingOptions添加到options的children里面
                    options.getChildren().add(buildingOptions);
                }
            }
            //一层循环组装一个商圈
            treeOptions.add(options);
        }
        return AjaxResult.success(treeOptions);
    }






}
